public class BinarySearchTree {

    class TreeNode{
        TreeNode left;
        TreeNode right;
        int val;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    TreeNode root = null;
    /**
     * 查找一个val 是不是在当前的搜索树当中
     * @param val
     * @return 有返回 没有返回null
     */
    public TreeNode search(int val) {
        if(root == null){
            return null;
        }

        TreeNode cur = root;

        while(cur != null){
            if(cur.val > val){
                cur = cur.left;
            } else if (cur.val < val) {
                cur = cur.right;
            }else{
                return cur;
            }
        }
        return null;
    }

    /**
     * 插入元素key
     * @param key
     * @return
     */
    public boolean insert(int key) {
        if(root == null){
            root = new TreeNode(key);
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null){
            if(cur.val > key){
                parent = cur;
                cur = cur.left;
            }else if(cur.val < key){
                parent = cur;
                cur = cur.right;
            }else{
                return false;
            }
        }

        if(parent.val > key){
            parent.left = new TreeNode(key);
        }else{
            parent.right = new TreeNode(key);
        }
        return true;
    }

    public void inorder(TreeNode root) {
        if(root == null){
            return;
        }
        TreeNode cur = root;

        inorder(cur.left);
        System.out.print(cur.val + " ");
        inorder(cur.right);
    }

    public void remove(int key) {
        if(root == null){
            return;
        }
        TreeNode parent = null;
        TreeNode cur = root;

        while(cur != null){
            if(cur.val == key){
                removeNode(parent,cur);
                return;
            }else if(cur.val > key){
                cur = parent;
                cur = cur.left;
            }else if(cur.val < key){
                cur = parent;
                cur = cur.right;
            }
        }
    }

    private void removeNode(TreeNode parent,TreeNode cur) {
        if(cur.left == null){
            if(root == cur){
                root = root.right;
            }else if(cur == parent.left){
                parent.left = cur.right;
            }else if(cur == parent.right){
                parent.right = cur.right;
            }
        }else if(cur.right ==null){
            if(root == cur){
                root = root.left;
            }else if(cur == parent.left){
                parent.left = cur.left;
            }else if(cur == parent.right){
                parent.right = cur.left;
            }
        }else{
            TreeNode tmp = cur.left;
            TreeNode tmpParent = cur;
            while(tmp.right != null){
                tmpParent = tmp;
                tmp = tmp.right;
            }
            cur.val = tmp.val;

            if(tmpParent == cur){
                tmpParent.left = null;
            }else {
                tmpParent.right = null;
            }

        }
    }
}
